# -*- coding: utf-8 -*-

import sys
from PyQt5.QtWidgets import (QApplication, QWidget, QToolTip, QPushButton, QMessageBox, QDesktopWidget)
from PyQt5.QtGui import (QIcon, QFont)
from PyQt5.QtCore import QCoreApplication

class Example(QWidget):
    '''面向对象的方式写界面
    '''
    def __init__(self,):
        super().__init__()
        self.initUi() # 自定义的绘制界面


    def initUi(self):
        '''画ui
        '''
        # 放一个按钮 并给他绑定一个提示
        btn = QPushButton('Button', self)
        btn.setToolTip('This is a <b>QPushButton</b> widget')
        btn.resize(btn.sizeHint())
        btn.move(50, 50)

        # 退出按钮
        btnQuite = QPushButton('Quite', self)
        # 绑定单击事件命令
        btnQuite.clicked.connect(QCoreApplication.instance().quit)
        btnQuite.move(150, 50)

        # 设置窗口位置大小
        # self.setGeometry(300, 100, 500, 300)
        self.resize(500, 300)
        self.center()
        self.setWindowTitle('OOP写界面')
        # BUG: 未成功
        self.setWindowIcon(QIcon('./asset/heart_503px.png'))
        self.show()


    def closeEvent(self, event):
        '''退出事件函数（生命周期）
        '''
        reply = QMessageBox.question(self, 'Message', "Are you sure to Quit ?",
            QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
        if reply == QMessageBox.Yes:
            event.accept()
        else:
            event.ignore()


    def center(self):
        '''将窗口居中
        '''
        qr = self.frameGeometry()
        cp = QDesktopWidget().availableGeometry().center()
        qr.moveCenter(cp)
        self.move(qr.topLeft())


if __name__ == "__main__":
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
